<template>
  <div>
    <Title />
    <el-container>
      <el-main>
        <el-card class="card" shadow="always">
          <el-form ref="setNewPwdRef" :model="setNewPwd" :rules="setNewPwdRules">
            <h3>忘记密码</h3>
            <el-form-item>
              <el-input :value="setNewPwd.phone" disabled />
            </el-form-item>
            <el-form-item prop="newPwd">
              <el-input v-model="setNewPwd.newPwd" placeholder="请输入新密码" />
            </el-form-item>
            <el-form-item>
              <el-button type="success" class="btn-login" @click="submit">确定</el-button>
            </el-form-item>
            <p style="font-size:12px;">记起密码？<router-link to="/login" style="color:#3CB371;">立即登录</router-link></p>
          </el-form>
        </el-card>
      </el-main>
    </el-container>
    <el-footer />
  </div>
</template>

<script>
import Title from '@/components/title.vue'
import '@/static/css/login.css'
import {setNewPwd} from '@/api/user'

export default {
  name: 'SetNewPassword',
  components: { Title },
  data() {
    return {
      setNewPwd: {
        phone: this.$route.params.phone,
        newPwd: ''
      },
      setNewPwdRules: {
        newPwd: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submit() {
      this.$refs.setNewPwdRef.validate(async valid => {
        if (!valid) return
        const res = await setNewPwd(this.setNewPwd.phone,this.setNewPwd.newPwd)
        if (res.code === 200) {
          this.$message.success('设置成功')
          this.$router.push('/login')
        } else {
          this.$message.error('设置失败')
        }
      })
    }
  }
}
</script>
<style scoped>
.card{
    margin:0 auto;
    width:20%;
    padding:20px 20px 10px 20px;
}
</style>
